package com.haven123.cloudapp.gateway.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Tony_Du on 2017/12/27.
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Value("${swagger.host}")
    String swaggerHost;

    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .build();
        //自定义服务的域名
        if (!StringUtils.isBlank(swaggerHost)) {
            docket.host(swaggerHost);
        }
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("app")
                .description("eden接口文档")
//                .termsOfServiceUrl("http://localhost/")
                .version("1.0")
                .build();
    }

    @Primary
    @Component
    class DocumentationConfig implements SwaggerResourcesProvider {
        @Override
        @SuppressWarnings("all")
        public List<SwaggerResource> get() {
            return Arrays.asList(
                    swaggerResource("app", "/app/v2/api-docs", "2.0")
            );
        }

        @SuppressWarnings("all")
        private SwaggerResource swaggerResource(String name, String location, String version) {
            SwaggerResource result = new SwaggerResource();
            result.setName(name);
            result.setLocation(location);
            result.setSwaggerVersion(version);
            return result;
        }
    }
}
